<template>
  <div class="login">
    <div class="admintite">
      <img src="../assets/images/loginlogo.png">
      <span>AM库存管理系统</span>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="flex1">
        <span class="title">让我们为您登录</span>
       <span class="title1">让我们为您登录</span>
      </div>

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="手机号"
          class="inputsize"
        >
          <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          :type="flag?'password':'text'"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          class="inputsize"
        >
        <i slot="suffix"
        v-if="flag"
        class="iconfont icon-biyan2"
        style="padding-top:6px;font-size:18px;margin-right: 12px;display: block;"
        autocomplete="auto"
        @click="flag=!flag"/>
        <i slot="suffix"
        v-else
        class="el-icon-view"
        style="margin-top:14px;font-size:18px;margin-right: 12px;"
        autocomplete="auto"
        @click="flag=!flag"/>

          <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          class="inputsize"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;color: rgba(118, 132, 146, 1);">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
        class="btnlogin"
          :loading="loading"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      flag:true,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
@font-face {
  font-family: "iconfont"; /* Project id 4388573 */
  src: url('//at.alicdn.com/t/c/font_4388573_2ptcrs5fi2a.woff2?t=1703562146939') format('woff2'),
       url('//at.alicdn.com/t/c/font_4388573_2ptcrs5fi2a.woff?t=1703562146939') format('woff'),
       url('//at.alicdn.com/t/c/font_4388573_2ptcrs5fi2a.ttf?t=1703562146939') format('truetype');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size:18px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-biyan1:before {
  content: "\e746";
}

.icon-biyan2:before {
  content: "\e67f";
}

.icon-yanjing-zhengyan:before {
  content: "\e63a";
}

.icon-biyan:before {
  content: "\e668";
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/loginbg.png");
  background-size: cover;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.admintite{
  display: flex;
  align-items: center;
  margin-bottom: 19px;
>img{
  width: 56px;
  height: 56px;
}
>span{
  padding-left: 15px;
  font-size: 24px;
font-weight: 900;
color: #6082F4;
}
}
.flex1{
  display: flex;
  flex-direction: column;
}
.title {
  margin: 0px auto 0px auto;
  text-align: center;
  color: rgba(1, 31, 59, 1);
  font-size: 22px;
}
.title1{
  margin: 12px auto 33px auto;
  text-align: center;
  color: rgba(143, 146, 161, 1);
  font-size: 14px;
}
::v-deep .el-checkbox__inner{
  background-color: #DEEBFF;
  border: none;
}
.btnlogin{
  height: 40px;
opacity: 1;
border-radius: 16px;
background: linear-gradient(90deg, rgba(95, 130, 245, 1) 0%, rgba(214, 171, 255, 1) 100%);
border: none;
color: #fff;
&:hover{
  color: #fff;
}
}
.login-form {
  background: rgba(255, 255, 255, 1);
  box-shadow: 80px 120px 504px  rgba(61, 116, 255, 0.16);
  width:360px;
  padding: 4.25rem 2rem 3rem 2rem;
  border-radius: 18px;
  .el-input {
    width: 100%;
    height: 46px;
    border-radius: 16px;
    background: rgba(245, 245, 251, 1);

    input {
      height: 46px;
      background: rgba(245, 245, 251, 1);
      border: none;
      border-radius: 16px;
      color: rgba(118, 132, 146, 1);
      padding-left: 24px;
      box-sizing: border-box;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  margin-top: 5px;
  width: 33%;
  height: 38px;
  float: right;
  border-radius: 16px;
//background: #F5F5FB;
height:46px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
